import { defineStore } from "pinia";
import { userLogin } from "@a/index";
import { showSuccessToast, showFailToast } from "vant";
import Cookies from "js-cookie";
import router from "@/router";
const useUserStore = defineStore('user', {
  state: () => {
    return {
      token: Cookies.get('token') || '',
      uid: Cookies.get('uid') || ''
    }
  },
  actions: {
    userLog(user: {username:string, pwd:string}){
      userLogin({
        ...user,
        deviceId: 1234,
        deviceName: "aaaa",
      }).then((res) => {
        if (res.data.code === 0) {
          // 将token 和 uid 存储到 pinia 同时 持久化
          Cookies.set('token', res.data.data.token, {
            expires: 7
          })
          Cookies.set('uid', res.data.data.uid, {
            expires: 7
          })
          showSuccessToast({
            message: '用户登录成功',
            onClose: () => {
              router.replace('/home')
            }
          })
        } else {
          showFailToast({
            message: res.data.msg,
          });
        }
      });
    }
  }
})

export default useUserStore